package com.happyscan138.makefun.base;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import static com.lidroid.xutils.bitmap.core.BitmapDecoder.calculateInSampleSize;

/**
 * Created by Administrator on 2016/12/30.
 */

public class bitmap {
    /**
     * 根据路径获得突破并压缩返回bitmap用于显示
     *
     * @param
     * @return
     */
    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        options.inSampleSize = calculateInSampleSize(options, 600, 960);
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }
    /**
     * 把Bitmap保存到SD卡中
     *
     * @param mBitmap
     */
    public static File saveMyBitmap(Bitmap mBitmap,String path) {

        File pictureFileDir = new File(Environment.getExternalStorageDirectory()+path);
        if (!pictureFileDir.exists()) {
            pictureFileDir.mkdirs();
        }
        File file = new File(pictureFileDir,  ".jpg");
        try {
            file.createNewFile();
        } catch (IOException e) {
            System.out.println("在保存图片时出错：" + e.toString());
        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if(mBitmap==null){
            return null;
        }else {
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 60, fOut);
        }
        try {
            fOut.flush();
            fOut.close();
        }  catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}
